<?php

class ChannelModel {

    protected $redis_master = '';
    protected $redis_slave = '';
    private $hash_name = 'channel_info';
    private $set_name = 'channel_set_info';
    private $hash_app_name = 'app_channel_info';
    private $set_app_name = 'app_channel_set_info';
    public function __construct() {
        $this->_config = Yaf_Application::app()->getConfig();
        $config_master = $this->_config->redis->params->master->toArray();
        $this->redis_master = new cache_redis($config_master);
        //$redis_master->redis()->setOption(Redis::OPT_PREFIX, 'Music');
        $config_slave = $this->_config->redis->params->slave->toArray();
        $this->redis_slave = new cache_redis($config_slave);
        // var_dump($this->redis_master->getMethods());die;

    }

    /*
     * 所有用户信息显示
     *
     */

    public function lists() {
        $lists_hash = $this->redis_slave->redis()->hGetAll($this->hash_name);
        $lists_set  = $this->redis_slave->redis()->zRevRange($this->set_name, 0, -1); 
        $lists = array();
        foreach ($lists_set as $key => $value) {
            $lists[$value] = $lists_hash[$value];
        }
        return $lists;
    }
    public function app_lists() {
        $lists_hash = $this->redis_slave->redis()->hGetAll($this->hash_app_name);
        $lists_set  = $this->redis_slave->redis()->zRevRange($this->set_app_name, 0, -1); 
        $lists = array();
        foreach ($lists_set as $key => $value) {
            $lists[$value] = $lists_hash[$value];
        }
        return $lists;
    }
    
    public function count() {
        $data = $this->redis_slave->redis()->hKeys($this->hash_name);
        return $data;
    }

    public function search_projects($keywod) {
        $this->sql = "SELECT * FROM `{$this->_table}` WHERE  project_name like '%{$keywod}%'";
        return $this->_db->getAll($this->sql);
    }

    /*
     * 添加新用户
     *
     */
    public function check_val($val) {
        $data = $this->redis_slave->redis()->hVals($this->hash_name);    
        if(in_array($val,$data )){
            return true;
        }else{
            return false;
        }
    }

    public function save($data) {
        if (!empty($data)) {
            if ($this->redis_master->redis()->hSetnx($this->hash_name,$data['channel_number'],$data['channel_name']) &&$this->redis_master->redis()->zAdd($this->set_name,intval(time()),$data['channel_number'] ) ) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    public function app_save($data) {
        if (!empty($data)) {
            if ($this->redis_master->redis()->hSetnx($this->hash_app_name,$data['app_number'],$data['channel_name']) &&$this->redis_master->redis()->zAdd($this->set_app_name,intval(time()),$data['app_number'] ) ) {
                return true;
            } else {
                return false;
            }
        }
    }

    /*
     * 删除用户
     *
     */

    public function del($key) {       
        if($this->redis_master->redis()->hDel($this->hash_name, $key) &&$this->redis_master->redis()->zDelete($this->set_name,$key ) ){
            return true;
        }else{
            return false;
        }
    }
    
    public function app_del($key) {       
        if($this->redis_master->redis()->hDel($this->hash_app_name, $key) &&$this->redis_master->redis()->zDelete($this->set_app_name,$key ) ){
            return true;
        }else{
            return false;
        }
    }

    /*
     * 获取用户信息
     *
     */

    public function get_info($key) {
        $info = array($key);      
        $info[] = $this->redis_slave->redis()->hGet($this->hash_name,$key);
        return $info;
    }
    
    public function get_app_info($key) {
        $info = array($key);      
        $info[] = $this->redis_slave->redis()->hGet($this->hash_app_name,$key);
        return $info;
    }

    /*
     * 修改用户信息
     *
     */

    public function update($data) {
        $key = $data['channel_number'];
        $val = $data['channel_name'];
        if($this->redis_slave->redis()->hexists($this->hash_name,$key)){
            if ($this->redis_master->redis()->hSet($this->hash_name,$key, $val)==0) {
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
        
        
    }
    
    public function app_update($data) {
        $key = $data['app_number'];
        $val = $data['channel_name'];

        if($this->redis_slave->redis()->hexists($this->hash_app_name,$key)){
            if ($this->redis_master->redis()->hSet($this->hash_app_name,$key, $val)==0) {
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
        
        
    }
    
 

}
